# Build recipe for musl.
#
# Copyright (c) 2015-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=musl
version=c00cdefa1da17f60b3179704528582ef320e61b8
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/libs"

tarname=${program}-${version}.tar.gz

# Remote source(s)
#fetch=http://www.musl-libc.org/releases/$tarname
#
# This is for musl snapshot versions.
#
fetch=http://git.musl-libc.org/cgit/musl/snapshot/$tarname

description="
A powerful standard C/POSIX library.

Musl is a new standard library to power a new generation of Linux-based
devices.  Musl is lightweight, fast, simple, free, and strives to be
correct in the sense of standards-conformance and safety.
"

homepage=http://www.musl-libc.org
license="MIT Expat variant"

# Source documentation
docs="COPYRIGHT README VERSION WHATSNEW"
docsdir="${docdir}/${program}"

# Limit package name to the program name
full_pkgname=$program

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    patch -Np0 -i "${worktree}/patches/musl/musl-utmp_path.diff"

    # Do not use the compiler runtime library from the temporary system,
    # the toolchain must be adjusted until the native GCC take place
    if cc -print-libgcc-file-name | grep -q '^/tools'
    then
        patch -Np1 -i "${worktree}/patches/musl/musl-nolibcc_stage1.diff"
    fi

    # Some tune options for i586, x86_64
    patch -Np1 -i "${worktree}/patches/musl/musl-tune.diff"

    ./configure $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --syslibdir=/lib \
     --enable-shared \
     --enable-static \
     --enable-optimize=size

    make -j${jobs}
    make -j${jobs} install DESTDIR="$destdir"

    # To print shared library dependencies
    mkdir -p "${destdir}/usr/bin"
    ln -sf /usr/lib${libSuffix}/libc.so "${destdir}/usr/bin/ldd"

    # Create dynamic linker runtime file taking -$(ARCH) as reference
    for file in "${destdir}"/lib/ld-musl-*.so.1
    do
        if test -e "$file"
        then
            ld_path="${file##*/}"		# Basename
            ld_path="${ld_path%%.*}.path"	# Get the rid of .so.1
            break;
        fi
    done
    unset file
    if test -n "$ld_path"
    then
        mkdir -p "${destdir}/etc"
        cat << EOF > "${destdir}"/etc/$ld_path
/lib
/usr/local/lib
/usr/lib
/usr/$(cc -dumpmachine)/lib

EOF
        chmod 644 "${destdir}"/etc/$ld_path
        touch "${destdir}/etc/.graft-config"
    else
        echo "WARNING: \$ld_path is empty." 1>&2
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

